package com.wjb.web.backend;

import com.wjb.model.EbChildProductCategory;
import com.wjb.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/ebProductCategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 分页显示信息
     * @param model
     * @param page 页码
     * @param pageSize 页码显示的最大数据量
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, @RequestParam( value = "page",defaultValue = "1")Integer page,@RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize )
    {
        List<EbChildProductCategory> ebChildProductCategories = productCategoryService.listByPage(page,pageSize);
        model.addAttribute("ebProductCategoryList",ebChildProductCategories);
        Integer pageTotal = productCategoryService.getMaxPage(pageSize);
        model.addAttribute("pageTotal",pageTotal);
        model.addAttribute("curPage",page);
        return "manage/productClass";
    }

    /**
     * 增加分类的跳转操作
     * @return  要跳转到的页面
     */
    @RequestMapping("/save")
    public String save(Model model)
    {
           List<EbChildProductCategory> oneParent = productCategoryService.oneParent();
           model.addAttribute("oneParent",oneParent);
        return "manage/productClass-add";
    }
    @RequestMapping("/saveCommit")
    public String saveCommit(EbChildProductCategory eCPC)
    {
                if (eCPC.getEpcParentId()==null||eCPC.getEpcName().trim().length()==0)
                {
                    return "manage/paramError";
                }
                   int result;
                if (eCPC.getEpcParentId().equals(0))
                {
                    result = productCategoryService.saveParent(eCPC);
                }
                 else
                     result = productCategoryService.saveChild(eCPC);
                if (result<=0)
                {
                    return "manage/paramError";
                }
              return "manage/manage-result";
    }
    @RequestMapping("/edit")
    public String edit(Model model, Integer id,Integer page)
    {
         EbChildProductCategory eCPC = productCategoryService.selectOne(id);
         EbChildProductCategory eCPC2 = productCategoryService.selectOne(eCPC.getEpcParentId());
         if (!eCPC.getEpcId().equals(eCPC2.getEpcId()))
         {
             eCPC.setParent(eCPC2);
         }
         model.addAttribute("eCPC",eCPC);
        model.addAttribute("curPage",page);
        return "manage/productClass-modify";

    }
    @RequestMapping("/editSubmit")
    public  String editSubmit(Model model,EbChildProductCategory eCPC,Integer page)
    {
              int result = productCategoryService.updateECPC(eCPC);
              if (result<=0)
                  return "manage/paramError";
         model.addAttribute("curPage",page);
         return "manage/manage-result";
    }

    @RequestMapping("/delect")
    public String delect(Model model,Integer id,@RequestParam( value = "page",defaultValue = "1") Integer page,@RequestParam( value = "pageSize",defaultValue = "1") Integer pageSize
    )
    {
        int temp = productCategoryService.delectECPC(id);
        int pageTotal = productCategoryService.getMaxPage(pageSize);
        if (page>pageTotal)
            page = pageTotal;
        model.addAttribute("curPage",page);
        if (temp<=0)
        {
            return "manage/paramError";
        }
        return  "manage/manage-result";
    }
}
